package com.like.likeutils.util;

import org.xutils.x;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.like.likeutils.R;
import com.like.likeutils.device.ScreenUtils;

public class MyDialogFragment extends DialogFragment {

	public static final int MATCH_PARENT = -1;
	public static final int WRAP_CONTENT = -2;

	public static final int DEFAULT_GRAVITY = -100;

	private int mWidth;
	private int mHeight;
	private int mGravity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CommonDialog);
		onSetPosition();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View v = x.view().inject(this, inflater, container);
		onCreateView();
		return v;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		x.view().inject(this, this.getView());
	}

	protected int getScreenWidth() {
		return ScreenUtils.getScreenWidth(getContext());
	}

	protected int getScreenHeight() {
		return ScreenUtils.getScreenHeight(getContext());
	}

	protected void onCreateView() {
	}
	
	protected void onSetPosition() {
	}

	protected void setPosition(float width, float height, int gravity) {
		if (width == MATCH_PARENT)
			mWidth = getScreenWidth();
		else if (width > 0 && width < 1)
			mWidth = (int) (getScreenWidth() * width);
		else
			mWidth = (int) width;
		if (height == MATCH_PARENT)
			mHeight = getScreenHeight();
		else if (height > 0 && height < 1)
			mHeight = (int) (getScreenHeight() * height);
		else
			mHeight = (int) height;
		mGravity = gravity;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mGravity != DEFAULT_GRAVITY)
			getDialog().getWindow().setGravity(mGravity);
		if (mWidth == WRAP_CONTENT)
			mWidth = getDialog().getWindow().getAttributes().width;
		if (mHeight == WRAP_CONTENT)
			mHeight = getDialog().getWindow().getAttributes().height;
		getDialog().getWindow().setLayout(mWidth, mHeight);
	}

}
